from django import forms
from .models import *


class PhoneForm(forms.ModelForm):
    class Meta:
        model = Phone
        fields = ['phonenumber']
        labels = {
            'phonenumber': '手机号码',
        }
        widgets = {
            'phonenumber': forms.TextInput(attrs={
                'class': 'form-control',
                'placeholder': '请输入11位手机号码',
                'maxlength': '11'
            })
        }

    def clean_phonenumber(self):
        phonenumber = self.cleaned_data.get('phonenumber')
        if phonenumber:
            existing_phones = Phone.objects.filter(phonenumber=phonenumber)
            if existing_phones.exists():
                pass
        return phonenumber

class PaymentForm(forms.ModelForm):
    """缴费表单"""
    class Meta:
        model = Payment
        fields = ['amount', 'pay_method', 'description']
        labels = {
            'amount': '缴费金额(元)',
            'pay_method': '缴费方式',
            'description': '备注',
        }
        widgets = {
            'amount': forms.NumberInput(attrs={
                'class': 'form-control',
                'placeholder': '请输入缴费金额',
                'min': '0.01',
                'step': '0.01'
            }),
            'pay_method': forms.Select(attrs={'class': 'form-control'}),
            'description': forms.TextInput(attrs={
                'class': 'form-control',
                'placeholder': '可选，添加备注信息'
            })
        }

    def clean_amount(self):
        amount = self.cleaned_data.get('amount')
        if amount and amount <= 0:
            raise forms.ValidationError('缴费金额必须大于0')
        return amount

class ComboSelectionForm(forms.Form):
    """套餐选择表单"""
    plan = forms.ModelChoiceField(
        queryset=Combo.objects.filter(is_active=True),
        empty_label="请选择套餐",
        label='选择套餐',
        widget=forms.Select(attrs={'class': 'form-control'})
    )